import _ from 'lodash'
import {AppNavigator} from '../routers'
//初始化页面
// const initialState = AppNavigator.router.getStateForAction(AppNavigator.router.getActionForPathAndParams('Welcome'))

export default function nav(state , action) {
  let nextState,
    routes,
    key
  let actionType = action.type
  if (action.key) {
    routes = state.routes || []
    if (_.find(routes, {routeName: action.key})) {
      action.key = (_.find(routes, {routeName: action.key}))['key']
    }
  }

  if (actionType.indexOf('Navigation') >= 0 || actionType.indexOf('redux') >= 0) {

    nextState = AppNavigator.router.getStateForAction(action, state)

  }
  switch (actionType) {
    case 'Navigation/NAVIGATE':
      routes = nextState.routes
      let currentRoutes = state.routes
      let index = nextState.index
      let lastRoute = _.last(routes)
      let lastCurrentRoute = _.last(currentRoutes)
      //重复跳转问题
      if (lastRoute && lastCurrentRoute && (lastRoute.routeName == lastCurrentRoute.routeName) && lastCurrentRoute.routeName != 'Root') {
        nextState = state
      }
      break
  }
  // Simply return the original `state` if `nextState` is null or undefined.
  return nextState || state
}
